<?php
/**
*  课程
*/
namespace app\api\model;
use think\Model;

class CourseMenu extends Model
{
    /**
    *  列表
    */
    public function _list($where = '', $order_by="a.sort asc", $page_size = PAGE_SIZE_API){
        $data = $this->alias('a')->where($where)
            ->field('a.*, l.title as lecturer')
            ->join('lecturer l', 'l.id= a.lid', 'LEFT')
            ->order($order_by)
            ->paginate(100)
            ->toArray();
        return $data;
    }
    
    /**
    * 通过视频cid获取
    * @param sid int 课程id
    * @return array
    */
    public function _getByCid($cid){
        $data = $this->alias('a')
            ->field('a.id, a.cid, a.lid, a.is_new, c.sid,a.link,a.duration, c.title, c.content')
            ->join('course c', 'c.id= a.cid', 'LEFT')
            ->where([['a.cid', '=', $cid]])
            ->order("a.sort asc")
            ->find();
        
        return $data;
    }
    
    /**
    * 通过章节id获取
    * @param id int 章节id
    * @return array
    */
    public function _getInfoByID($id){
        $data = $this->alias('a')
            ->field('a.id, a.cid, a.is_new, a.lid, a.link,a.duration, c.title, c.content')
            ->join('course c', 'c.id= a.cid', 'LEFT')
            ->where([['a.id', '=', $id]])
            ->find();
        
        return $data;
    }
    
    /**
    * 通过id与cid获取
    */
    public function _getInfoByIdAndCid($id, $cid){
        $data = $this->alias('a')
            ->field('a.id, a.cid, a.is_new, a.lid, a.link,a.duration, c.title, c.content')
            ->join('course c', 'c.id= a.cid', 'LEFT')
            ->where([['a.id', '=', $id], ['a.cid', '=', $cid]])
            ->find();
        
        return $data;
    }
    
    /**
    * 通过视频sid获取
    * @param sid int 科目id
    * @return array
    */
    public function _getBySid($sid){
        $data = $this->alias('a')
            ->field('a.id, a.cid, a.is_new,a.lid, a.link,a.duration, c.title, c.content')
            ->join('course c', 'c.id= a.cid', 'LEFT')
            ->where([['c.sid', '=', $sid]])
            ->order("a.sort asc")
            ->find();
        
        return $data;
    }
    
    /**
    * 通过条件获取所有课时
    */
    public function _getCourseTimeAll($where){
        return $this->where($where)->sum('duration');
    }

}